package org.xinghai.sql.common.api;


import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;
    private Long timestamp = System.currentTimeMillis();

    private Result() {}

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> failed(IResultCode resultCode) {
        return failed(resultCode, null);
    }

    public static <T> Result<T> failed(IResultCode resultCode, T data) {
        Result<T> result = new Result<>();
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> failed(String message) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.FAILED.getCode());
        result.setMessage(message);
        return result;
    }

    // 新增静态方法
    public static <T> Result<T> unauthorized(T data) {
        return failed(ResultCode.UNAUTHORIZED, data);
    }

    public static <T> Result<T> forbidden(T data) {
        return failed(ResultCode.FORBIDDEN, data);
    }
}